{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "31ae3a94-7cae-40c5-b494-68ea023bf681",
   "metadata": {},
   "source": [
    "## Synthetic Data for Data Augmentation Using A Decoder-Style LLM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "51461d61-1d5c-4b26-ad8f-053b46b2a545",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Author: Sebastian Raschka\n",
      "\n",
      "Python implementation: CPython\n",
      "Python version       : 3.10.6\n",
      "IPython version      : 8.12.0\n",
      "\n",
      "transformers: 4.27.2\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%load_ext watermark\n",
    "%watermark -a 'Sebastian Raschka' -v -p transformers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bd8f113d-56c5-492b-ac07-2105169c7f15",
   "metadata": {},
   "outputs": [],
   "source": [
    "from transformers import GPT2LMHeadModel, GPT2Tokenizer\n",
    "\n",
    "\n",
    "def generate_synthetic_text(prompt, num_samples=1):\n",
    "    model_name = \"gpt2\"\n",
    "    model = GPT2LMHeadModel.from_pretrained(model_name)\n",
    "    tokenizer = GPT2Tokenizer.from_pretrained(model_name)\n",
    "    \n",
    "    synthetic_texts = []\n",
    "    for _ in range(num_samples):\n",
    "        inputs = tokenizer(prompt, return_tensors=\"pt\")\n",
    "        input_ids = inputs[\"input_ids\"]\n",
    "        attention_mask = inputs[\"attention_mask\"]\n",
    "\n",
    "        sample_output = model.generate(\n",
    "            input_ids,\n",
    "            max_length=100,  # You can set this to control the length of generated text\n",
    "            min_length=30,   # Minimum length of the generated text\n",
    "            num_return_sequences=1,\n",
    "            attention_mask=attention_mask,\n",
    "            no_repeat_ngram_size=2, # This will prevent repeating n-grams (here 2-grams) in the generated text\n",
    "            early_stopping=True\n",
    "        )\n",
    "\n",
    "        text = tokenizer.decode(sample_output[0], skip_special_tokens=True)\n",
    "        synthetic_texts.append(text)\n",
    "    \n",
    "    return synthetic_texts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2333612b-7fe3-4c38-9608-09945df78bcd",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The weather was nice and I enjoyed the view. I was able to get a good view of the city and the surrounding area. The weather is good and it was a nice day.\n",
      "\n",
      "I was very impressed with the views. It was the first time I've been to the area and was really impressed. We had a great time and we were able get to see the entire city. There was also a lot of parking and there was plenty of traffic. Parking is very easy and you can\n"
     ]
    }
   ],
   "source": [
    "# Example prompt\n",
    "prompt = \"The weather was nice and I enjoyed\"\n",
    "\n",
    "# Generate synthetic data\n",
    "synthetic_data = generate_synthetic_text(prompt)\n",
    "for text in synthetic_data:\n",
    "    print(text)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
